<template>
  <!--销售订单审核-->
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="销售订单号">
              <a-input placeholder="请输入销售订单号" v-model="queryParam.serial"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="客户">
              <j-multi-select-tag placeholder="请选择客户" v-model="queryParam.customId" dictCode="hf_custom,name,id" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="客户订单号">
              <a-input v-model="queryParam.customOrderSerial" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="单据日期">
              <j-date
                type="list"
                v-model="queryParam.date"
                @change="handleDateInterval"
                style="width: 100%"
                :disabled="queryParam.dateInterval"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="日期区间">
              <a-range-picker
                v-model="queryParam.dateInterval"
                @change="handleDateInterval"
                :disabled="queryParam.documentDate"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
      <a-button type="primary" @click="searchReset" icon="reload">重置</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('销售订单')">导出</a-button>
      <a-button type="primary" icon="calculator" @click="handleMPR" style="background-color:green;border-color: green"
        >生成采购单</a-button
      >
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :customRow="clickThenSelect"
        @change="handleTableChange"
      >
        <template slot="isClosed" slot-scope="text">
          <a-tag v-if="text === 0" color="red">未关闭</a-tag>
          <a-tag v-else color="green">关闭</a-tag>
        </template>
        <template slot="status" slot-scope="text">
          <a-tag :color="text === '未审核' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>

        <span slot="action" slot-scope="text, record">
          <a-divider type="vertical" v-if="record.status === 1" />
          <a @click="handleUndoCheck(record)" v-if="record.status === 1">反审核</a>
          <a-divider type="vertical" v-if="record.status === 1" />
          <a @click="handleCheck(record)" v-if="record.status === 0">审核</a>
          <a-divider type="vertical" v-if="record.status === 0" />
          <a @click="handleClose(record)" v-if="record.isClosed === 0">关闭</a>
          <a @click="handleUndoClose(record)" v-if="record.isClosed === 1">反关闭</a>
        </span>
      </a-table>
    </div>

    <a-tabs defaultActiveKey="1">
      <a-tab-pane tab="销售订单明细" key="1">
        <HfSaleorderDetailList
          :mainId="selectedMainId"
          :customId="customId"
          :source-serial="selectedSerial"
          ref="DetailList"
        />
      </a-tab-pane>
    </a-tabs>

    <hfSaleorder-modal ref="modalForm" @ok="modalFormOk"></hfSaleorder-modal>
    <HfPurchaseSuggestModal ref="HfPurchaseSuggestModal"></HfPurchaseSuggestModal>
    <MRPModal :sale-order-ids="saleOrderIds" ref="modalMRPForm"></MRPModal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfSaleorderModal from './modules/HfSaleOrderModal.vue'
import HfPurchaseSuggestModal from './modules/HfPurchaseSuggestModal'
import { getAction } from '@/api/manage'
import HfSaleorderDetailList from './HfSaleOrderDetailList.vue'
import HfPurchaseRequestDetailList from '@views/purchase/HfPurchaseRequestDetailList.vue'
import MRPModal from '@views/sale/modules/MRPModal.vue'
import HfGenerateSaleOutModal from '@views/sale/modules/HfGenerateSaleOutModal.vue'
import HfSaleOrderChart from '@views/sale/charts/HfSaleOrderChart.vue'

export default {
  name: 'HfSaleorderCheckList',
  mixins: [JeecgListMixin],
  components: {
    HfSaleOrderChart,
    HfGenerateSaleOutModal,
    MRPModal,
    HfPurchaseRequestDetailList,
    HfSaleorderDetailList,
    HfSaleorderModal,
    HfPurchaseSuggestModal
  },
  data() {
    return {
      description: '销售订单管理页面',
      queryParam: {
        status: 1
      },
      // 表头
      columns: [
        {
          title: '销售订单号',
          align: 'center',
          dataIndex: 'serial'
        },
        {
          title: '单据日期',
          align: 'center',
          dataIndex: 'documentDate'
        },

        {
          title: '客户订单号',
          align: 'center',
          dataIndex: 'customOrderSerial'
        },

        {
          title: '客户',
          align: 'center',
          dataIndex: 'customId_dictText'
        },

        {
          title: '结算日期',
          align: 'center',
          dataIndex: 'paymentTime'
        },
        {
          title: '制单',
          align: 'center',
          dataIndex: 'operator'
        },
        {
          title: '审核员',
          align: 'center',
          dataIndex: 'checker'
        },
        {
          title: '审核时间',
          align: 'center',
          dataIndex: 'checkTime'
        },
        {
          title: '审核状态',
          align: 'center',
          dataIndex: 'status_dictText',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '关闭状态',
          align: 'center',
          dataIndex: 'isClosed',
          scopedSlots: { customRender: 'isClosed' }
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/sale/hfSaleOrder/list',
        check: '/sale/hfSaleOrder/check',
        undoCheck: '/sale/hfSaleOrder/undoCheck',
        close: '/sale/hfSaleOrder/close',
        undoClose: '/sale/hfSaleOrder/undoClose'
      },
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 5,
        pageSizeOptions: ['5', '10', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      selectedMainId: '',
      superFieldList: [],
      superQueryFieldList: [],
      superQueryFlag: false,
      superQueryParams: '',
      customId: '',
      customOrderSerial: '',
      selectedSerial: '',
      saleOrderIds: ''
    }
  },
  created() {},
  watch: {
    selectedMainId: function(newVal, oldVal) {
      const target = this.dataSource.find(item => item.id === newVal)
      if (target) {
        this.customId = target.customId
        this.customOrderSerial = target.customOrderSerial
      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    handleDateInterval(value) {
      this.queryParam.documentDate_begin = value[0].format('YYYY-MM-DD')
      this.queryParam.documentDate_end = value[1].format('YYYY-MM-DD')
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      this.selectedSerial = selectionRows[0].serial
      this.selectedCustomId = selectionRows[0].customId
      this.$refs.DetailList.isChecked = true
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      this.onClearSelected()
      var params = this.getQueryParams() //查询条件
      params.status = 1
      this.loading = true
      getAction(this.url.list, params).then(res => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    handleMPR() {
      if (!this.selectionRows || this.selectionRows.length === 0) {
        this.$message.warning('请先选择单据')
        return
      }
      this.$refs.modalMRPForm.visible = true
      this.saleOrderIds = this.selectedRowKeys.join(',')
    },
    generateSaleOut(record) {}
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
